/**
 * Created by cyf on 15-12-1.
 * webSocket处理
 */
(function() {
    qxchat.QXWebSocket = QXWebSocket;
    var EXTEND = null;

    function QXWebSocket() {
        if (EXTEND) {
            EXTEND.apply(this, arguments);
        }
        this._ws = null;
        var self = this;
        this.eventHandler = new qxchat.EventHandler();
        //建立连接
        this.connect = function() {
                if ('WebSocket' in window) {
                    self._ws = new WebSocket("ws://" + global.serverPath + ":" + global.serverPort + "/webSocketServer?loginName=" + global.currentUserLoginName);
                } else if ('MozWebSocket' in window) {
                    self._ws = new MozWebSocket("ws://" + global.serverPath + ":" + global.serverPort + "/webSocketServer?loginName=" + global.currentUserLoginName);
                } else {
                    self._ws = new SockJS("http://" + global.serverPath + ":" + global.serverPort + "/sockjs/webSocketServer?loginName=" + global.currentUserLoginName);
                }
                //连接成功后执行
                self._ws.onopen = function() {
                    self._log('Info: connection opened.');
                };
                //连接出错后执行
                self._ws.onerror = function() {
                    layer.closeAll('loading');
                    layer.msg('连接失败',{icon: 2});
                    self._log('Info: connection error.');
                };
                //接收的消息
                self._ws.onmessage = function(event) {
                    //交给事件处理器
                    self.eventHandler.handle(event.data)
                    self._log('Received: ' + event.data);
                };
                //连接关闭后执行
                self._ws.onclose = function(event) {
                    $('#qx-mainBody').css("display", "none");
                    $('#qx-loginBody').css("display", "inline");
                    self._log('Info: connection closed.');
                    self._log(event);
                };
            }
            //登录
        this.login = function(password) {
                var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","password":"' + password + '","type":"Login",},"type":"event"}';
                self._log('Sent: ' + message);
                self._ws.send(message);
            }
            //读取好友记录
        this.getFirends = function() {
                var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","type":"Firends",},"type":"event"}';
                self._log('Sent: ' + message);
                self._ws.send(message);
            }
            //发送消息 格式：{"data":{"from":"user1","msg":"hahaha","to":"user2"},"type":"msg"}
        this.send = function(message) {
                if (self._ws != null) {
                    self._log('Sent: ' + message);
                    self._ws.send(message);
                } else {
                    alert('connection not established, please connect.');
                }
            }
            //关闭连接
        this.disconnect = function() {
                if (self._ws != null) {
                    self._ws.close();
                    self._ws = null;
                }
            }
            //log
        this._log = function(message) {}

    }

})();